//
//  TaskListView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/21.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "TaskListView.h"
#import "TaskListViewCell.h"

@interface TaskListView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *list_table_view;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *taskData;
@property (nonatomic, assign) BOOL isRefresh;

@end

@implementation TaskListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self makeTaskListView];
        
    }
    return self;
}

- (void)makeTaskListView
{
    UIView *tabView = [UIView new];
    [self addSubview:tabView];
    [tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    
    UIButton *mine_task_button = [UIButton new];
    mine_task_button.layer.masksToBounds = YES;
    mine_task_button.layer.borderColor = [[UIColor blackColor] CGColor];
    mine_task_button.layer.borderWidth = 1.0;
    mine_task_button.layer.cornerRadius = 5.0;
    mine_task_button.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [mine_task_button setTitle:@"我的任务" forState:UIControlStateNormal];
    [mine_task_button addTarget:self action:@selector(clickMineTaskButton:) forControlEvents:UIControlEventTouchUpInside];
    [mine_task_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [tabView addSubview:mine_task_button];
    [mine_task_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(6);
        make.bottom.mas_equalTo(-6);
        make.right.mas_equalTo(-14);
        make.width.mas_equalTo(80);
    }];
    
    _taskData = [NSMutableArray array];
    _isRefresh = YES;
    
    _list_table_view = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _list_table_view.dataSource = self;
    _list_table_view.delegate = self;
    _list_table_view.separatorStyle = UITableViewCellSeparatorStyleNone;
//    _list_table_view.layer.masksToBounds = YES;
//    _list_table_view.layer.borderWidth = 1.0;
//    _list_table_view.layer.borderColor = [RGB(240, 240, 240) CGColor];
    _list_table_view.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_list_table_view];
    [_list_table_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(tabView.mas_top).mas_offset(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    __weak __typeof(self) weakSelf = self;
    _list_table_view.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        weakSelf.isRefresh = YES;
        if ([self.delegate respondsToSelector:@selector(headerRefresh:)]) {
            [self.delegate headerRefresh:weakSelf.page];
        }
    }];
    
    [self headerRefreshListData];
    
    _list_table_view.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page++;
        weakSelf.isRefresh = NO;
        if ([self.delegate respondsToSelector:@selector(footerLoadMore:)]) {
            [self.delegate footerLoadMore:weakSelf.page];
        }
    }];
}

- (void)headerRefreshListData
{
    [_list_table_view.mj_header beginRefreshing];
}

- (void)loadTaskListData:(NSArray *)listData
{
    if (_isRefresh == YES) {
        [_list_table_view.mj_header endRefreshing];
        [_list_table_view.mj_footer endRefreshing];
        [self.taskData removeAllObjects];
    }else{
        if (listData.count == 0) {
            [_list_table_view.mj_footer endRefreshingWithNoMoreData];
        }else{
            [_list_table_view.mj_footer endRefreshing];
        }
    }
    [self.taskData addObjectsFromArray:listData];
    [_list_table_view reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.taskData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TaskListViewCell *listCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TaskListViewCell class])];
    if (!listCell) {
        listCell = [[TaskListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([TaskListViewCell class])];
    }
    
    [listCell loadTaskListCell:(TaskModel *)[self.taskData objectAtIndex:indexPath.row]];
    __weak __typeof(self) weakSelf = self;
    listCell.selectCellBlock = ^(){
        [weakSelf clickListButtonAtIndex:indexPath.row];
    };
    
    return listCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)clickListButtonAtIndex:(NSInteger)index
{
    TaskModel *model = (TaskModel *)[self.taskData objectAtIndex:index];
    if ([self.delegate respondsToSelector:@selector(didSelectTask:index:)]) {
        [self.delegate didSelectTask:model index:index];
    }
}

- (void)clickMineTaskButton:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(clickMineTaskList)]) {
        [self.delegate clickMineTaskList];
    }
}

@end
